import {jsToast} from './common.js' // 封装的uni.showToast

const request = ({
	url,
	data,
	method
}) => {
	let token = uni.getStorageSync('token')
	let headers = {
		'content-type': 'application/json',
	}
	if (token !== null && token !== '' && token !== undefined) {
		headers.Authorization = "Bearer " + token //存储token
	}
	// let api_url = uni.getStorageSync('ipAddress');
	// let api_url = 'https://test.qdhezheng.cn/svr'
	let api_url = 'https://jt.tsingtaomuseum.com/svr'

	return new Promise((resolve, reject) => {
		uni.request({
			url: api_url + url,
			method: method,
			timeout: 300000,
			data: data,
			header: headers,
		}).then((response) => {
			if (response[1]) {
				let res = response[1];
				if (res.statusCode == 200) {
					resolve(res.data)
					if (res.data.status != 200) {
						jsToast(res.data.message,'none')
					}
				} else {
					if (res.statusCode == 404 || res.statusCode == 405) {
						jsToast('接口'+res.statusCode+'，请联系开发人员','error')
					}
				}
			} else {
				jsToast(response[0].errMsg,'error')
			}
		}).catch(error => {
			console.log(error)
		})
	})
}
export default request